package com.lagou.dao;

import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @program: IPersistence_test
 * @description:
 * @author: wyw
 * @create: 2020-01-02 16:41
 */
public class IUserDaoImpl implements IUserDao {
    @Override
    public List<User> findAll() throws Exception {
        //代码重复, 硬编码
        InputStream resourcesAsStream = Resources.getResourcesAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourcesAsStream);
        SqlSession sqlSession = build.openSession();
        List<User> list = sqlSession.selectList("user.selectList");
        return list;
    }

    @Override
    public User finByCondition(User user) throws Exception {
        InputStream resourcesAsStream = Resources.getResourcesAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourcesAsStream);
        SqlSession sqlSession = build.openSession();
        return sqlSession.selectOne("user.selectOne", user);
    }

    @Override
    public int add(User user) throws Exception {
        //用getMapper代理了接口类，这个方法不使用
        return 0;
    }

    @Override
    public int updateByCondition(User user) throws Exception {
        //用getMapper代理了接口类，这个方法不使用
        return 0;
    }

    @Override
    public int deleteByCondition(User user) throws Exception {
        //用getMapper代理了接口类，这个方法不使用
        return 0;
    }
}
